<?php
require __DIR__.'/language.class.php';
header("Content-type: text/html; charset=utf-8");
session_start();
function bug($obj){
    echo '<pre>';
    print_r($obj);
    echo '</pre>';
}

//配置访问密码
$password = '123456';
$is_login = empty($_SESSION['language_manager_login'])?'':$_SESSION['language_manager_login'];
if($is_login != 1){
    $input_password = empty($_POST['password'])?'':$_POST['password'];
    if($input_password == $password){
        $_SESSION['language_manager_login'] = 1;
    }else{
        //没有登录
        exit('<!DOCTYPE html>
        <html>
            <head><title>提示</title></head>
            <body>
            <form action="manager.php" method="post" >
            <input type="password" name="password" />
            <input type="submit" value="提交" />
            </form>
            </body>
        </html>');
    }
}


$type = isset($_REQUEST['type'])?$_REQUEST['type']:'';
$lang_dir = __DIR__.'/data';
$lang_list = dir_dir($lang_dir);
$context = '';
$refresh_flag = false;
$insert_success = false;
$put_result = false;
$put_save_result = false;
switch ($type){
    case 'show':
        $lang_type = isset($_POST['lang_type'])?$_POST['lang_type']:'';
        $context = isset($_POST['context'])?$_POST['context']:'';
        if(!empty($context)){
            //获取语言文件
            $show_lang_dir = $lang_dir.'/'.$lang_type;
            $dir_list = dir_list($show_lang_dir);
            //查询到所属的语言
            $langs = select_word($dir_list,$show_lang_dir,$context);
            //得到要查询的key
            $keys = array();
            foreach ($langs as $sel_key){
                $keys[] = $sel_key['key'];
            }
            $result_langs[$lang_type] = $langs;
            $lang_list_show =array();
            //查询其它语言的值
            //移除要查询的语言值
            foreach ($lang_list as $list_show){
                if($list_show != $lang_type){
                    $lang_list_show[] = $list_show;
                }
            }
            foreach ($lang_list_show as $lang){
                $show_lang_dir = $lang_dir.'/'.$lang;
                $dir_list = dir_list($show_lang_dir);
                $select_word_key_result = select_word_key($dir_list,$show_lang_dir,$keys);
                $result_langs[$lang] = $select_word_key_result;
            }
        }
        break;
    case 'refresh':
        foreach ($lang_list as $lang){
            Language::set_obj_null();
            Language::set_lang($lang);
        }
        $refresh_flag = true;
        break;
    case 'insert':
        $i_context = array();
        $key = get_key();
        foreach ($lang_list as $list){
            if(empty(trim($_POST[$list]))){
                echo $list.':不能为空 <a href=\'javascript:window.history.back()\'>返回...</a>';
                exit;
            }
            $i_context[$list] = $_POST[$list];
        }
        //写入文件
        foreach ($lang_list as $list){
            $lang_file_path = $lang_dir.'/'.$list.'/auto_add.ini';
            //组织语言
            $set_word = $key.' = '.$i_context[$list];
            if(file_put_contents($lang_file_path,$set_word."\r\n", FILE_APPEND | LOCK_EX) !== false){
                $insert_success = true;
            }
        }
        break;
    case 'del':
        $del_key = trim($_GET['key']);
        foreach ($lang_list as $list){
            //获取语言文件
            $show_lang_dir = $lang_dir.'/'.$list;
            $dir_list = dir_list($show_lang_dir);

            //一个一个文件找key
            foreach ($dir_list as $search_item){
                $put_path = $show_lang_dir.'/'.$search_item;
                $words = parse_ini_file($put_path);
                if(array_key_exists($del_key,$words)){
                    $white_text = '';
                    foreach ($words as $key=>$val){
                        if($key != $del_key){
                            $white_text.=$key.'="'.$val."\"\r\n";
                        }
                    }
                    $put_result = file_put_contents($put_path, $white_text, LOCK_EX);
                }
            }
        }
        break;
    case 'edit':
        $edit_key = trim($_GET['key']);

        $lang_array = array();
        foreach ($lang_list as $list){
            //获取语言文件
            $show_lang_dir = $lang_dir.'/'.$list;
            $dir_list = dir_list($show_lang_dir);
            foreach ($dir_list as $dlist){
                $lang_word = parse_ini_file($show_lang_dir.'/'.$dlist);
                if(array_key_exists($edit_key,$lang_word)){
                    $lang_array[$list] = array($edit_key=>$lang_word[$edit_key]);
                }
            }
        }
        break;
    case 'save':
        $edit_key = $_POST['edit_key'];
        $i_context = array();
        foreach ($lang_list as $list){
            if(empty(trim($_POST[$list]))){
                echo $list.':不能为空 <a href=\'javascript:window.history.back()\'>返回...</a>';
                exit;
            }
            $i_context[$list] = $_POST[$list];
        }

        foreach ($lang_list as $list){
            $show_lang_dir = $lang_dir.'/'.$list;
            $dir_list = dir_list($show_lang_dir);
            //一个一个文件找key
            foreach ($dir_list as $search_item){
                $put_path = $show_lang_dir.'/'.$search_item;
                $words = parse_ini_file($put_path);
                if(array_key_exists($edit_key,$words)){
                    $white_text = '';
                    foreach ($words as $key=>$val){
                        if($key != $edit_key){
                            $white_text.=$key.'="'.$val."\"\r\n";
                        }else{
                            $white_text.=$edit_key.'="'.$i_context[$list]."\"\r\n";
                        }
                    }
                    $put_save_result = file_put_contents($put_path, $white_text, LOCK_EX);
                }
            }
        }

        break;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>语言代码生成器</title>
        <style>
            *{font-size: 12px;}
            h1,h2,h3,h4,h5,h6,p,span{margin: 0;padding: 0;}
            .main th{ font-size: 12px;background: #f4f4f4;}
            .main td{ text-align: center;border: 1px solid #f4f4f4;line-height: 20px;}
            .main tr .lang{ text-align: left;text-indent:2em;}
            .red{color: red;}
        </style>
    </head>
<body>
    <?php if($type == 'edit'){ ?>
        <form method="post" action="manager.php?type=save" >
            <input type="hidden" name="edit_key" value="<?=$edit_key?>" />
            <table>
                <Tr>
                    <td></td>
                    <td>编辑词语</td>
                </Tr>
                <?php foreach ($lang_array as $lang_edit_key=>$edit_val){ ?>
                    <Tr>
                        <td><?=$lang_edit_key?>:</td>
                        <td><input type="text" name="<?=$lang_edit_key?>" value="<?=$edit_val[$edit_key]?>" /></td>
                    </Tr>
                <?php } ?>
                <Tr>
                    <td></td>
                    <td><input type="submit" value="提交编辑" /></td>
                </Tr>
            </table>
        </form>
    <?php } ?>

    <?php if($type == 'add'){ ?>
        <form method="post" action="manager.php?type=insert" >
            <table>
                <?php foreach ($lang_list as $list){ ?>
                    <Tr>
                        <td><?=$list?>:</td>
                        <td><input type="text" name="<?=$list?>" /></td>
                    </Tr>
                <?php } ?>
                <Tr>
                    <td></td>
                    <td><input type="submit" value="添加" /></td>
                </Tr>
            </table>
        </form>
    <?php } ?>
    <table><tr><td colspan="2">
                <?php if($put_result){ ?><span class="red">删除成功！</span><?php } ?>
                <?php if($put_save_result){ ?><span class="red">编辑成功！</span><?php } ?>
                <?php if($insert_success){ ?><span class="red">添加成功！key值为：<?=$key?></span><?php } ?>
            </td></tr></table>

    <form method="post" action="manager.php?type=show" >
        <table>
            <Tr>
                <td>语言:</td>
                <td><select name="lang_type">
                        <?php foreach ($lang_list as $list){ ?>
                        <option value="<?=$list?>"><?=$list?></option>
                        <?php } ?>
                    </select></td>
                <td>内容</td>
                <td><input type="text" value="<?=$context?>" name="context" /></td>
                <td><input type="submit" value="查询" /></td>
                <td><input type="button" onclick="add_lang()" value="新增" /></td>
                <td><input type="button" onclick="refresh_cache()" value="刷新缓存" /> <?php if($refresh_flag){ ?><span class="red">刷新成功!</span><?php } ?></td>

            </Tr>
        </table>
    </form>
    <table class="main" width="100%">
        <tr>
            <th width="15%">语言代码</th>
            <?php foreach ($lang_list as $list){ ?>
            <th ><?=$list?></th>
            <?php } ?>
            <th width="8%">操作</th>
        </tr>
        <?php if(!empty($result_langs)){ ?>
            <?php foreach (current($result_langs) as $langs_val_key=>$langs_val_val){ ?>
                <tr>
                    <td><?=$langs_val_val['key']; ?></td>
                    <?php foreach ($lang_list as $list){ ?>
                        <?php foreach ($result_langs as $langs_key=>$langs_val){?>
                            <?php if($langs_key == $list){?>
                                <td class="lang"><?=empty($langs_val[$langs_val_key]['val'])?'无':$langs_val[$langs_val_key]['val']?></td>
                            <?php } ?>
                        <?php } ?>
                    <?php } ?>
                    <td><a href="javascript:edit_lang('<?=$langs_val_val['key']; ?>')">修改</a> | <a href="javascript:del_lang('<?=$langs_val_val['key']; ?>')">删除</a></td>
                </tr>
            <?php } ?>
        <?php } ?>
    </table>
<script>
    function refresh_cache(){
        location.href = 'manager.php?type=refresh';
    }
    function add_lang() {
        location.href = 'manager.php?type=add';
    }
    function del_lang(key){
        if(confirm('确定要删除吗?')){
            location.href = 'manager.php?type=del&key='+key;
        }
    }
    function edit_lang(key){
        location.href = 'manager.php?type=edit&key='+key;
    }
</script>
</body>
</html>
